package com.retail.xiaocui.server.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.retail.xiaocui.common.domain.Purchase;
import com.retail.xiaocui.common.domain.Warehouse;
import com.retail.xiaocui.common.domain.req.AddPurchaseReq;
import com.retail.xiaocui.server.service.PurchaseService;
import com.retail.xiaocui.server.service.WarehouseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.retail.common.log.annotation.Log;
import com.retail.common.log.enums.BusinessType;
import com.retail.common.security.annotation.RequiresPermissions;
import com.retail.xiaocui.common.domain.Procurement;
import com.retail.xiaocui.server.service.ProcurementService;
import com.retail.common.core.web.controller.BaseController;
import com.retail.common.core.domain.Result;
import com.retail.common.core.utils.poi.ExcelUtil;
import com.retail.common.core.web.page.TableDataInfo;

/**
 * 采购需求申请Controller
 *
 * @author retail
 * @date 2024-03-02
 */
@RestController
@RequestMapping("/procurement")
public class ProcurementController extends BaseController
{
    @Autowired
    private ProcurementService procurementService;

    /**
     * 采购单
     */
    @Autowired
    private PurchaseService purchaseService;

    /**
     * 仓库
     */
    @Autowired
    private WarehouseService warehouseService;



    /**
     * 查询采购需求申请列表
     */
    @RequiresPermissions("xiaocui:procurement:list")
    @GetMapping("/list")
    public TableDataInfo list(Procurement procurement)
    {
        startPage();
        List<Procurement> list = procurementService.selectProcurementList(procurement);
        return getDataTable(list);
    }

    /**
     * 导出采购需求申请列表
     */
    @RequiresPermissions("xiaocui:procurement:export")
    @Log(title = "采购需求申请", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Procurement procurement)
    {
        List<Procurement> list = procurementService.selectProcurementList(procurement);
        ExcelUtil<Procurement> util = new ExcelUtil<Procurement>(Procurement.class);
        util.exportExcel(response, list, "采购需求申请数据");
    }

    /**
     * 获取采购需求申请详细信息
     */
    @RequiresPermissions("xiaocui:procurement:query")
    @GetMapping(value = "/{id}")
    public Result getInfo(@PathVariable("id") Long id)
    {
        return success(procurementService.selectProcurementById(id));
    }

    /**
     * 新增采购需求申请
     */
    @RequiresPermissions("xiaocui:procurement:add")
    @Log(title = "采购需求申请", businessType = BusinessType.INSERT)
    @PostMapping
    public Result add(@RequestBody Procurement procurement)
    {
        return toAjax(procurementService.insertProcurement(procurement));
    }

    /**
     * 修改采购需求申请
     */
    @RequiresPermissions("xiaocui:procurement:edit")
    @Log(title = "采购需求申请", businessType = BusinessType.UPDATE)
    @PutMapping
    public Result edit(@RequestBody Procurement procurement)
    {
        return toAjax(procurementService.updateProcurement(procurement));
    }

    /**
     * 删除采购需求申请
     */
    @RequiresPermissions("xiaocui:procurement:remove")
    @Log(title = "采购需求申请", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public Result remove(@PathVariable Long[] ids)
    {
        return toAjax(procurementService.deleteProcurementByIds(ids));
    }


    /**
     * 合并整单请求
     */
    @RequiresPermissions("xiaocui:procurement:combines")
    @Log(title = "采购需求申请", businessType = BusinessType.COMBINE)
    @PostMapping("/{ids}")
    public Result mange(@PathVariable Long[] ids)
    {
        return toAjax(procurementService.combineProcurementByIds(ids));
    }

    /**
     * 查询采购单的id
     * @return
     */
    @GetMapping("/purchaseList")
    public Result purchaseList(){
        List<Purchase> list = purchaseService.purchaseList();
        return Result.success(list);
    }

    /**
     * 传一个数组进行创建一个新的采购单
     */
    @PostMapping("/addPurchase")
    public Result addPurchase(@RequestBody AddPurchaseReq addPurchaseReq){
        return purchaseService.addPurchase(addPurchaseReq);
    }

    /**
     * 查看仓库中的信息 用于下拉框
     */
    @GetMapping("/warehouseList")
    public Result<List<Warehouse>> warehouseList(){
        return warehouseService.warehouseList();
    }





}
